package com.config.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.base.dao.TemplateDaoImpl;
import com.config.entity.EquStateConfig;


@Repository(value="equStateConfigDao")
public class EquStateConfigDaoImpl extends TemplateDaoImpl<EquStateConfig,Integer> implements EquStateConfigDao{
	/**
	 * 根据设备状态获取对应的功率范围
	 * @param state
	 * @return
	 */
	@Override
	public EquStateConfig getEquPowerValue(String stateType) {
		String sql="select * from equStateConfig where stateType=:stateType";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("stateType", stateType);
		return jt.queryForObject(sql, params, ParameterizedBeanPropertyRowMapper.newInstance(EquStateConfig.class));
	}
	/**
	 * 根据功率值获取设备状态
	 * @param power
	 * @return
	 */
	@Override
	public EquStateConfig getEquStateType(double power) {
		String sql="select * from equStateConfig where lowValue<=:power and highValue>:power";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("power", power);
		return jt.queryForObject(sql, params, ParameterizedBeanPropertyRowMapper.newInstance(EquStateConfig.class));
	}
	@Override
	public List<EquStateConfig> getDistinctType() {
		StringBuilder sql = new StringBuilder("select distinct(stateType) from equStateConfig ");
		Map<String, Object> params=new HashMap<String, Object>();
		return jt.query(sql.toString(), params, ParameterizedBeanPropertyRowMapper.newInstance(EquStateConfig.class));
	}
	@Override
	public List<EquStateConfig> findAll(int start, int limit) {
		StringBuilder sb = new StringBuilder("select top "+limit+" * from (")
		.append("select row_number() over(order by id desc ) rowindex,* from ")
		.append(" equStateConfig where 1=1");
		Map<String, Object> params=new HashMap<String, Object>();
		sb.append(") u where u.rowindex>:start");
		params.put("start", (start-1)*limit);
		params.put("limit", limit);
		return jt.query(sb.toString(),params,
				ParameterizedBeanPropertyRowMapper.newInstance(EquStateConfig.class));
	}
	@Override
	public int countbyFindAll() {
		StringBuffer sb = new StringBuffer("select count(*) from equStateConfig where 1=1");
		Map<String, Object> params=new HashMap<String, Object>();
		return jt.queryForObject(sb.toString(), params, Integer.class);
	
	}
}
